package com.yipei.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlTradeAccountFinancing;
import com.yipei.business.mapper.MlTradeAccountFinancingMapper;
import com.yipei.business.service.IMlAssetFlowService;
import com.yipei.business.service.IMlTradeAccountFinancingService;
import com.yipei.business.service.IMlTradeAccountService;
import com.yipei.common.core.domain.AjaxResult;
import com.yipei.common.core.domain.entity.MlTradeAccount;
import com.yipei.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账号融资Service业务层处理
 *
 * @author yipei
 * @date 2024-12-23
 */
@Service
public class MlTradeAccountFinancingServiceImpl extends ServiceImpl<MlTradeAccountFinancingMapper, MlTradeAccountFinancing> implements IMlTradeAccountFinancingService
{
    @Autowired
    private MlTradeAccountFinancingMapper mlTradeAccountFinancingMapper;

    @Autowired
    private IMlTradeAccountService iMlTradeAccountService;

    @Autowired
    private IMlAssetFlowService iMlAssetFlowService;
    /**
     * 查询账号融资
     *
     * @param id 账号融资主键
     * @return 账号融资
     */
    @Override
    public MlTradeAccountFinancing selectMlTradeAccountFinancingById(Long id)
    {
        return mlTradeAccountFinancingMapper.selectMlTradeAccountFinancingById(id);
    }

    /**
     * 查询账号融资列表
     *
     * @param mlTradeAccountFinancing 账号融资
     * @return 账号融资
     */
    @Override
    public List<MlTradeAccountFinancing> selectMlTradeAccountFinancingList(MlTradeAccountFinancing mlTradeAccountFinancing)
    {
        return mlTradeAccountFinancingMapper.selectMlTradeAccountFinancingList(mlTradeAccountFinancing);
    }

    /**
     * 新增账号融资
     *
     * @param mlTradeAccountFinancing 账号融资
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMlTradeAccountFinancing(MlTradeAccountFinancing mlTradeAccountFinancing) throws Exception {
        mlTradeAccountFinancing.setCreateTime(DateUtils.getNowDate());
        mlTradeAccountFinancing.setHbJe(BigDecimal.ZERO);
        mlTradeAccountFinancing.setHxJe(BigDecimal.ZERO);
        mlTradeAccountFinancing.setYyEd(BigDecimal.ZERO);
        BigDecimal rlx = mlTradeAccountFinancing.getAlimit().multiply(mlTradeAccountFinancing.getRate().divide(new BigDecimal(100)));
        mlTradeAccountFinancing.setZlxJe(rlx);
        mlTradeAccountFinancing.setHkZt("0");
        LambdaQueryWrapper<MlTradeAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MlTradeAccount::getTdAcc,mlTradeAccountFinancing.getTdAcc());
        MlTradeAccount one = iMlTradeAccountService.getOne(lambdaQueryWrapper);
        if (ObjectUtils.isEmpty(one)){
            throw new Exception("融资账号不对,请重新输入");
        }
        one.setCash(one.getCash().add(mlTradeAccountFinancing.getAlimit()));
        iMlTradeAccountService.updateById(one);
        mlTradeAccountFinancingMapper.insertMlTradeAccountFinancing(mlTradeAccountFinancing);
        return   1;
    }

    /**
     * 修改账号融资
     *
     * @param mlTradeAccountFinancing 账号融资
     * @return 结果
     */
    @Override
    public int updateMlTradeAccountFinancing(MlTradeAccountFinancing mlTradeAccountFinancing)
    {
        mlTradeAccountFinancing.setUpdateTime(DateUtils.getNowDate());
        return mlTradeAccountFinancingMapper.updateMlTradeAccountFinancing(mlTradeAccountFinancing);
    }

    /**
     * 批量删除账号融资
     *
     * @param ids 需要删除的账号融资主键
     * @return 结果
     */
    @Override
    public int deleteMlTradeAccountFinancingByIds(Long[] ids)
    {
        return mlTradeAccountFinancingMapper.deleteMlTradeAccountFinancingByIds(ids);
    }

    /**
     * 删除账号融资信息
     *
     * @param id 账号融资主键
     * @return 结果
     */
    @Override
    public int deleteMlTradeAccountFinancingById(Long id)
    {
        return mlTradeAccountFinancingMapper.deleteMlTradeAccountFinancingById(id);
    }

    @Override
    @Transactional
    public AjaxResult repayment(MlTradeAccountFinancing mlTradeAccountFinancing) throws Exception {
        MlTradeAccountFinancing financing = this.selectMlTradeAccountFinancingById(mlTradeAccountFinancing.getId());
        BigDecimal thisRepayment = mlTradeAccountFinancing.getThisRepayment();
        MlTradeAccount mlTradeAccount = iMlTradeAccountService.lambdaQuery().eq(MlTradeAccount::getTdAcc, financing.getTdAcc()).one();
        if (thisRepayment.compareTo(mlTradeAccount.getKqZj())<=0){
            mlTradeAccount.setKqZj(mlTradeAccount.getKqZj().subtract(thisRepayment));
        }else if (thisRepayment.compareTo(mlTradeAccount.getDjCash())<=0){
            mlTradeAccount.setDjCash(mlTradeAccount.getDjCash().subtract(thisRepayment));
        }else if (thisRepayment.compareTo(mlTradeAccount.getCash())<=0){
            mlTradeAccount.setCash(mlTradeAccount.getCash().subtract(thisRepayment));
        }else {
            throw new Exception("交易账号："+financing.getTdAcc()+"剩余余额不足");
        }
        financing.setHbJe(financing.getHbJe().add(thisRepayment));
        this.updateMlTradeAccountFinancing(financing);
        iMlTradeAccountService.updateById(mlTradeAccount);
        iMlAssetFlowService.creatObject(mlTradeAccount.getTdAcc(), 6, thisRepayment.multiply(new BigDecimal(-1)));
        return AjaxResult.success("还本成功");
    }

    @Override
    @Transactional
    public AjaxResult payInterest(MlTradeAccountFinancing mlTradeAccountFinancing) throws Exception {
        MlTradeAccountFinancing financing = this.selectMlTradeAccountFinancingById(mlTradeAccountFinancing.getId());
        BigDecimal thisInterest = mlTradeAccountFinancing.getThisInterest();
        MlTradeAccount mlTradeAccount = iMlTradeAccountService.lambdaQuery().eq(MlTradeAccount::getTdAcc, financing.getTdAcc()).one();
        if (thisInterest.compareTo(mlTradeAccount.getKqZj())<=0){
            mlTradeAccount.setKqZj(mlTradeAccount.getKqZj().subtract(thisInterest));
        }else if (thisInterest.compareTo(mlTradeAccount.getDjCash())<=0){
            mlTradeAccount.setDjCash(mlTradeAccount.getDjCash().subtract(thisInterest));
        }else if (thisInterest.compareTo(mlTradeAccount.getCash())<=0){
            mlTradeAccount.setCash(mlTradeAccount.getCash().subtract(thisInterest));
        }else {
            throw new Exception("交易账号："+financing.getTdAcc()+"剩余余额不足");
        }
        financing.setHxJe(financing.getHxJe().add(thisInterest));
        if (financing.getHxJe().compareTo(financing.getZlxJe())==0){
            financing.setHkZt("1");
        }
        this.updateMlTradeAccountFinancing(financing);
        iMlTradeAccountService.updateById(mlTradeAccount);

        iMlAssetFlowService.creatObject(mlTradeAccount.getTdAcc(), 7, thisInterest.multiply(new BigDecimal(-1)));
        return AjaxResult.success("还息成功");
    }

    @Override
    public void interestSettlementJob() {
        List<MlTradeAccountFinancing> mlTradeAccountFinancings = this.lambdaQuery().eq(MlTradeAccountFinancing::getHkZt,"0").list();
         if (!CollectionUtils.isEmpty(mlTradeAccountFinancings)){
             for (MlTradeAccountFinancing financing : mlTradeAccountFinancings) {
                 /**
                  * 日利息
                  */
                     BigDecimal rlx = financing.getAlimit().multiply(financing.getRate().divide(new BigDecimal(100)));
                     financing.setZlxJe(financing.getZlxJe().add(rlx));

             }
         }
        this.updateBatchById(mlTradeAccountFinancings,200);
    }
}
